package com.zmn.oms.admin.controller.work.list;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.oms.common.constant.OmsPageConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.vo.TabQueryVO;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.oms.model.vo.work.list.ZsOrderWorkBranchFinanceVO;
import com.zmn.oms.zmn.business.interfaces.work.list.ZsOrderWorkBranchFinanceQueryBService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：子公司财务工单列表
 *
 * @author liuying
 * @date 2018/12/27 12:04
 */
@Controller
@RequestMapping("/branchfinance/work")
public class WorkBranchFinanceListController extends WorkListTabBaseController {

    @Autowired
    private ZsOrderWorkBranchFinanceQueryBService zsOrderWorkBranchFinanceQueryBService;

    /**
     * 工单列表
     */
    @RequiresPermissions("/zs/branchfinance/work/list.action")
    @RequestMapping(value = "/list")
    public ModelAndView list(OrderWorkQuery query, ModelAndView mv, String queryView) {
        mv.setViewName("work/list/branchfinance/list");

        // 默认显示查询条件
        if (Objects.isNull(queryView)) {
            queryView = "precisionQuery";
        }
        mv.addObject("hideQueryView", GlobalConsts.NO);

        super.setDayLimit(mv);
        super.setDefaultModelAndView(mv);

        // 查询待算账、待报销、本月开发票的数量
        List<TabQueryVO> tabList = Lists.newArrayList();

        // 精准查询
        if (StaffAdminService.isPermitted("/zs/branchfinance/work/list/precision/query.action")) {
            tabList.add(TabQueryVO.builder().tabType(OmsPageConsts.QUERY_TAB_PRECISION).tabTypeName(OmsPageConsts.QUERY_TAB_PRECISION_NAME)
                    .queryView(OmsPageConsts.QUERY_TAB_PRECISION_VIEW_NAME).autoQuery(GlobalConsts.NO).build());
        }
        // 待算账
        if (StaffAdminService.isPermitted("/zs/branchfinance/work/list/wait/account.action")) {
            tabList.add(TabQueryVO.builder().tabType(OmsPageConsts.QUERY_TAB_WAIT_ACCOUNT).tabTypeName(OmsPageConsts.QUERY_TAB_WAIT_ACCOUNT_NAME).build());
        }
        // 待报销
        if (StaffAdminService.isPermitted("/zs/branchfinance/work/list/wait/reimburse.action")) {
            tabList.add(TabQueryVO.builder().tabType(OmsPageConsts.QUERY_TAB_REIMBURSE).tabTypeName(OmsPageConsts.QUERY_TAB_WAIT_REIMBURSE_NAME).build());
        }
        // 本月开发票
        if (StaffAdminService.isPermitted("/zs/branchfinance/work/list/current/month/invoice.action")) {
            tabList.add(TabQueryVO.builder().tabType(OmsPageConsts.QUERY_TAB_INVOICE).tabTypeName(OmsPageConsts.QUERY_TAB_INVOICE_NAME).build());
        }

        // 下单日期，默认30天
        mv.addObject("startTime", DateUtil.toString(DateUtil.addDays(DateUtil.getNow(), -30), "yyyy-MM-dd 00:00:00"));
        mv.addObject("endTime", DateUtil.toString(DateUtil.getNow(), "yyyy-MM-dd 23:59:59"));
        super.setDayLimit(mv);

        mv.addObject("tabList", tabList);
        mv.addObject("queryView", queryView);

        // 默认查询条件
        mv.addObject("query", query);
        return mv;
    }

    /**
     * 工单列表 ajax
     */
    @RequiresPermissions("/zs/branchfinance/work/list.action")
    @PostMapping(value = "/ajax/list")
    @ResponseBody
    public ResultDTO ajaxList(@RequestBody OrderWorkQuery query) {
        // 处理TAB
        this.processTagTypeQuery(query);

        // 处理查询条件
        this.processSearchQuery(query);

        List<ZsOrderWorkBranchFinanceVO> voList = zsOrderWorkBranchFinanceQueryBService.listPageOrderWorkByQuery(query);
        LayuiTableDO<ZsOrderWorkBranchFinanceVO> data = new LayuiTableDO<>();
        data.setCount(query.getTotalCount());
        data.setData(voList);
        Map<String, Object> mapData = Maps.newHashMap();
//        mapData.put("masterCount", zsOrderWorkBranchFinanceQueryBService.findMasterCountByQuery(query));
        data.setExtData(mapData);
        return ResultDTO.success(data);
    }

    /**
     * 工单,单条 ajax
     */
    @RequiresPermissions("/zs/branchfinance/work/list.action")
    @GetMapping(value = "/ajax/row")
    @ResponseBody
    public ResultDTO ajaxRow(@Valid OrderWorkQuery query) {
        // 处理查询条件
        this.processSearchQuery(query);
        List<ZsOrderWorkBranchFinanceVO> voList = zsOrderWorkBranchFinanceQueryBService.listPageOrderWorkByQueryNoRecord(query);
        return ResultDTO.success(voList.get(0));
    }

    /**
     * 处理tab 查询
     *
     * @param query
     */
    @Override
    void processTagTypeQuery(OrderWorkQuery query) {
        Integer tabType = query.getTabType();
        if (tabType != null) {
            switch (tabType) {
                case OmsPageConsts.QUERY_TAB_WAIT_ACCOUNT:
                    // 待算账
                    query.setWorkStatus(Lists.newArrayList(OrderStatusConsts.WORK_STATUS_CHECKOUT));
                    break;
                case OmsPageConsts.QUERY_TAB_REIMBURSE:
                    // 待报销
                    query.setPartReimburseStatus(OrderConsts.PART_REIMBURSE_STATUS_DOING);
                    break;
                case OmsPageConsts.QUERY_TAB_INVOICE:
                    // 本月开票
                    query.setInvoiceTime(String.format("%s,%s", DateUtil.getNowFormatted("yyyy-MM-01 00:00:00"), DateUtil.getNowFormatted("yyyy-MM-dd 23:59:59")));
                    break;
                default:
                    // 不处理
            }
        }
    }

    /**
     * 处理查询条件
     *
     * @param query
     */
    private void processSearchQuery(OrderWorkQuery query) {
        // 脱敏
        // query.setIsSensitiveAddr(!StaffAdminService.isPermitted(OrderPremissionConsts.getOrderWorkListAddressPermitUrl(OrderPremissionConsts.MODULE_ID_ZS_BRANCHFINANCE)));
        query.setIsSensitiveAddr(true);
        query.setIsSensitiveTel(true);

        // 查询服务平台是啄木鸟的
        query.setPlatWork(GlobalConsts.PLAT_MARK_ZMN);

        // 查询手机号对应的用户
        query.setQueryConcatPhoneUsers(true);
    }
}